package com.qfedu.controller;

import com.qfedu.common.core.annotation.NoLogin;
import com.qfedu.common.core.common.R;
import com.qfedu.common.core.entity.Weather;
import com.qfedu.service.ITbWeatherService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 天气表控制器
 */
@RestController
@RequestMapping("/weather")
public class TbWeatherController {
    private static final Logger log = LoggerFactory.getLogger(TbWeatherController.class);

    @Resource
    private ITbWeatherService tbWeatherService;

    /**
     * 获取实时最新天气预报
     * @return 统一响应对象R包含天气预报数据
     */
    @NoLogin
    @ApiOperation(value="获取实时最新天气预报", notes="返回全部数据后端接口", response = Weather.class)
    @PostMapping("/getWeather")
    public R getWeather() {
        try {
            List<Weather> weatherData = tbWeatherService.getWeather();
            return R.ok(weatherData);
        } catch (Exception e) {
            log.error("获取天气数据失败", e);
            return R.fail(e.getMessage());
        }
    }
    /**
     * 手动触发天气数据导入（测试用）
     * @return 统一响应对象
     */
    @NoLogin
    @ApiOperation(value="手动导入天气数据", notes="用于测试天气数据导入接口")
    @PostMapping("/manualImport")
    public R manualImportWeather() {
        try {
            tbWeatherService.importWeather();
            return R.ok("天气数据导入已触发");
        } catch (Exception e) {
            log.error("手动导入天气数据失败", e);
            return R.fail(e.getMessage());
        }
    }
}